﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLine.Models;

namespace ZoneLine.Stations
{
    /// <summary>end
    /// 下叉臂拧紧
    /// </summary>
    public class QOP006Station : WorkStationDBase
    {
        public QOP006Station()
        {

        }

        public void Init(string ip, int port = 102, string startAddr = "DB55.0", string endUploadCodeAddr = "DB55.18.1", string endUploadDataAddr = "DB55.18.2", string allowWorkAddr = "DB55.20")
        {
            Ip = ip;
            Port = port;
            _startAddr = startAddr;
            _readLength = 247;

            EndUpLoadCodeAddr = endUploadCodeAddr;
            EndUpLoadDataAddr = endUploadDataAddr;
            AllowWorkAddr = allowWorkAddr;
            StationRecordData = new QOP006StationResult();
            HeartToPlcAddr = "DB55.18.0";
            _logUiChannel = "QOP006Station" + "uilog";
        }

        private QOP006StationResult _stationRecordData;

        public QOP006StationResult StationRecordData
        {
            get { return _stationRecordData; }
            set => SetProperty(ref _stationRecordData, value);
        }


        protected override void DealData(byte[] content)
        {
            StationRecordData.TightenTorque = _client.ByteTransform.TransSingle(content, 178);
            StationRecordData.TightenAngle = _client.ByteTransform.TransSingle(content, 182);
            StationRecordData.TightenPosition = _client.ByteTransform.TransSingle(content, 186);
            StationRecordData.CycleTime = _client.ByteTransform.TransSingle(content, 190);

            StationRecordData.AbsScanCode = GetPlcString(content, 26);
            StationRecordData.ArmScanCode = GetPlcString(content, 58);
            StationRecordData.NutScanCode = GetPlcString(content, 90);
            StationRecordData.BoltScanCode = GetPlcString(content, 122);
            StationRecordData.PartType = GetPlcString(content, 154);
            StationRecordData.Result = GetPlcString(content, 166);

            StationRecordData.ArmScanCode2 = GetPlcString(content, 200);
            StationRecordData.NutScanCode2 = GetPlcString(content, 232);
            StationRecordData.BoltScanCode2 = GetPlcString(content, 264);
        }

        protected override void DealWithSignal(string name)
        {
            if (name == nameof(StartUploadCode))
            {
                //todo 上传了条码 这时候验证是否要工作
                if (StartUploadCode == false)
                {
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}收到上传条码信息变为false");
                    WriteData(EndUpLoadCodeAddr, false);
                    WriteData(AllowWorkAddr, 0);
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}复位工作控制指令和复位上传条码完成指令");
                    return;
                }
                else
                {
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}工位结论验证,条码信息是{StationRecordData.AbsScanCode}");
                    var validataRlt = Validata(CurrentStationName, StationRecordData.AbsScanCode);
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}工位的验证结果是{validataRlt}");
                    var writeRlt = WriteWorkActionToPlc(AllowWorkAddr, validataRlt);
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}工位向下位机下发消息，下发{writeRlt}");
                    return;
                }
            }

            if (name == nameof(StartUploadData))
            {
                if (StartUploadData)
                {
                    //todo 上传数据 这时候将数据记录到数据库中
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}收到记录数据命令");
                    var rlt = RecordData();
                    WriteData(EndUpLoadDataAddr, rlt == 1);
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}记录数据完成");
                }
                else
                {
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}收到记录命令数据复位");
                    WriteData(EndUpLoadDataAddr, false);
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}复位数据记录命令");
                }
            }
        }

        protected override int RecordData()
        {
            var result = new QOP006StationResult()
            {
                AbsScanCode = StationRecordData.AbsScanCode,
                ArmScanCode = StationRecordData.ArmScanCode,
                BoltScanCode = StationRecordData.BoltScanCode,
                CycleTime = StationRecordData.CycleTime,
                NutScanCode = StationRecordData.NutScanCode,
                PartType = StationRecordData.PartType,
                Result = StationRecordData.Result,
                TightenAngle = StationRecordData.TightenAngle,
                TightenPosition = StationRecordData.TightenPosition,
                TightenTorque = StationRecordData.TightenTorque,
                ArmScanCode2 = StationRecordData.ArmScanCode2,
                BoltScanCode2= StationRecordData.BoltScanCode2,
                NutScanCode2= StationRecordData.NutScanCode2,
            };
            var writeRlt = false;
            if (_dal.CheckExist<QOP006StationResult>(p => p.AbsScanCode == result.AbsScanCode))
            {
                var beforeRlt = _dal.GetModel<QOP006StationResult>(p => p.AbsScanCode == result.AbsScanCode);
                result.Id = beforeRlt.Id;
                writeRlt = _dal.UpdateModelSuper<QOP006StationResult>(p => p.AbsScanCode == result.AbsScanCode, result);
            }
            else
            {
                writeRlt = _dal.AddModel(result);

            }
            return writeRlt ? 1 : 2;
        }
    }
}
